package uestc.lj.gulishop.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import uestc.lj.common.constant.AuthServerConstant;
import uestc.lj.common.exception.ErrorCode;
import uestc.lj.common.utils.R;
import uestc.lj.gulishop.auth.service.LoginService;
import uestc.lj.gulishop.auth.vo.UserLoginVO;
import uestc.lj.gulishop.auth.vo.UserRegistVO;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @Author:Crazlee
 * @Date:2021/6/9
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;


    /**
     * 注册页面发送验证码
     *
     * @param phone
     * @return
     */
    @GetMapping("/sms/sendcode")
    @ResponseBody
    public R sendCode(@RequestParam("phone") String phone) {
        boolean flag = loginService.sendCode(phone);
        if (flag) {
            return R.ok();
        } else {
            return R.error(ErrorCode.SMS_CODE_SEND_FREQUENTLY.getCode(), ErrorCode.SMS_CODE_SEND_FREQUENTLY.getMessage());
        }
    }


    /**
     * 注册流程
     *
     * @return
     */
    @PostMapping("/regist")
    public String regist(@Valid UserRegistVO userRegistVO, BindingResult result, RedirectAttributes redirectAttributes) {
        boolean success = loginService.regist(userRegistVO, result, redirectAttributes);
        if (!success) {
            return "redirect:http://auth.gulishop.com/reg.html";
        }
        // 注册成功后，返回登录页面
        return "redirect:http://auth.gulishop.com/login.html";
    }

    /**
     * 登录成功
     */
    @PostMapping("/login")
    public String login(UserLoginVO userLoginVO, RedirectAttributes redirectAttributes, HttpSession session) {

        // 远程登录

        boolean success = loginService.login(userLoginVO, redirectAttributes, session);
        if (success) {
            return "redirect:http://gulishop.com";
        } else {
            return "redirect:http://auth.gulishop.com/login.html";
        }
    }

    /**
     * 使用微博第三方登录成功的回调函数
     *
     * @return
     */
    @GetMapping("/oauth2.0/weibo/success")
    public String weiboLogin(@RequestParam("code") String code, HttpSession session, HttpServletResponse httpResponse) throws Exception {
        // 根据code换取accesstoken
        boolean success = loginService.weiboLogin(code, session, httpResponse);
        if (success) {
            return "redirect:http://gulishop.com";
        }
        return "redirect:http://authh.gulishop.com/login.html";
    }

    @GetMapping("/login.html")
    public String loginPage(HttpSession session) {
        Object user = session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (user == null) {
            return "login";
        }
        return "redirect:http://gulishop.com";
    }
}
